package com.lyp.activitys;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.ToastUtils;
import com.lyp.adapters.IndepenAdapter;
import com.lyp.jsonbean.IndependentBean;
import com.lyp.jsonbean.LoginBean;
import com.lyp.jsonbean.RenYuanBean;
import com.tjl.yangxixis.R;
import com.tjl.yangxixis.api.YangxixiApi;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

//自主分配
public class IndependentActivity extends Activity implements View.OnClickListener{
    private ImageView mIageView;
    private Button mButton;
    private RecyclerView mRecyclerView;
    private LinearLayoutManager mLinearLayoutManager;
    private IndepenAdapter mAdapter;
    private List<RenYuanBean.DataBean> mList = new ArrayList<>();
    private LoginBean.DataBean bean;
    private String ids;
    public static int index = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_independent);
        init();
    }

    public void init(){
        bean = (LoginBean.DataBean) getIntent().getExtras().get("databean");
        ids = getIntent().getStringExtra("ids");
        mIageView = (ImageView) findViewById(R.id.iv_indepen_back);
        mButton = (Button) findViewById(R.id.btn_order_confirm);
        mRecyclerView = (RecyclerView) findViewById(R.id.inpend_recycler);
        mLinearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mAdapter = new IndepenAdapter(mList,this);
        // 实例化 RecyclerView Adapter
        mRecyclerView.setAdapter(mAdapter);

        mIageView.setOnClickListener(this);
        mButton.setVisibility(View.VISIBLE);
        mButton.setOnClickListener(this);
        try {
            renyuan(bean.getC_id());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void renyuan(String c_id) throws IOException {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(YangxixiApi.APP_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        YangxixiApi github = retrofit.create(YangxixiApi.class);
        Call<RenYuanBean> call = github.getRenyuan(c_id);
        call.enqueue(new Callback<RenYuanBean>() {
            @Override
            public void onResponse(Call<RenYuanBean> call, final Response<RenYuanBean> response) {
                if (response.body().getResult()== 1 ) {
                    IndependentActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mList.addAll(response.body().getData());
                            mAdapter.notifyDataSetChanged();
                        }
                    });
                }else {
                    ToastUtils.showShortSafe(response.body().getMessage());
                }
            }
            @Override
            public void onFailure(Call<RenYuanBean> call, Throwable t) {
                ToastUtils.showShortSafe(t.getMessage());
            }
        });
    }


    //分配任务按钮
    private void independent(String id, final String ids) throws IOException {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(YangxixiApi.APP_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        YangxixiApi github = retrofit.create(YangxixiApi.class);
        Call<IndependentBean> call = github.getIndepen(id, ids);
        call.enqueue(new Callback<IndependentBean>() {
            @Override
            public void onResponse(Call<IndependentBean> call, final Response<IndependentBean> response) {
                if (response.body().getResult()== 1 ) {
                    IndependentActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            new MaterialDialog.Builder(IndependentActivity.this)
                                    .title("分配成功")
                                    .content("快去做任务吧!")
                                    .positiveText("确定")
                                    .canceledOnTouchOutside(false)
                                    .cancelable(false)
                                    .onPositive(new MaterialDialog.SingleButtonCallback(){
                                        @Override
                                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                            setResult(RESULT_OK);
                                            finish();
                                        }
                                    })
                                    .show();
                                }
                        });
                }else {
                    ToastUtils.showShortSafe("没有订单或选择人员失败!");
                }
            }
            @Override
            public void onFailure(Call<IndependentBean> call, Throwable t) {
                ToastUtils.showShortSafe(t.getMessage());
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_indepen_back:
                setResult(RESULT_CANCELED);
                finish();
                break;
            case R.id.btn_order_confirm:
                try {
                    if (index < 0) {
                        ToastUtils.showShortSafe("请先选择人员");
                        return;
                    }
                    independent(mList.get(index).getId(),ids);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}